package com.common.project.util;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName WrapperUtil
 * @Description
 * @Author jiangshicun@bicai365.com
 * @Date 2019/7/1 16:09
 */
public class WrapperUtil<T> {

    private T object;


    public static <T>QueryWrapper<T> getQueryWarapper(Map<String, Object> paramMap,String table){
        paramMap=Optional.ofNullable(paramMap).orElse(new HashMap<>());

        Map<String, Map<String, Object>> whereCondition = (Map<String, Map<String, Object>>) Optional.ofNullable(paramMap.get("whereCondition")).orElse(new HashMap<>());
        Map<String, Map<String,Object>> column = (Map<String, Map<String, Object>>) Optional.ofNullable(paramMap.get("column")).orElse(new HashMap<>());

        Map<String, Object> tableColumn = column.get(table);

        QueryWrapper<T> wrapper = new QueryWrapper<T>().allEq(tableColumn,true);
        doWhereCondition(whereCondition, wrapper);

        return wrapper;
    }

    public static <T>UpdateWrapper<T> getUpdateWrapper(Map<String, Object> paramMap,String table){
        paramMap=Optional.ofNullable(paramMap).orElse(new HashMap<>());

        Map<String, Map<String, Object>> whereCondition = (Map<String, Map<String, Object>>) Optional.ofNullable(paramMap.get("whereCondition")).orElse(new HashMap<>());
        Map<String, Map<String,Object>> column = (Map<String, Map<String, Object>>) Optional.ofNullable(paramMap.get("column")).orElse(new HashMap<>());

        Map<String, Object> tableColumn = column.get(table);

        UpdateWrapper<T> wrapper=new UpdateWrapper<T>();
        tableColumn.forEach((k,v)->wrapper.set(k,v));

        doWhereCondition(whereCondition, wrapper);

        return wrapper;
    }


    private static <T> void doWhereCondition(Map<String, Map<String, Object>> whereCondition, AbstractWrapper wrapper) {
        // like
        Map<String, Object> likeMap = Optional.ofNullable((whereCondition).get("like")).orElse(new HashMap<>());
        likeMap.forEach((k,v)->wrapper.like(k,v));
        // like left
        Map<String, Object> likeLeftMap = Optional.ofNullable((whereCondition).get("likeLeft")).orElse(new HashMap<>());
        likeLeftMap.forEach((k,v)->wrapper.likeLeft(k,v));
        //like right
        Map<String, Object> likeRight = Optional.ofNullable((whereCondition).get("likeRight")).orElse(new HashMap<>());
        likeRight.forEach((k,v)->wrapper.likeRight(k,v));
        // between
        Map<String, Object> betweenMap = Optional.ofNullable((whereCondition).get("between")).orElse(new HashMap<>());
        betweenMap.forEach((k,v)->wrapper.between(k,
                (k.endsWith("time")? DateUtil.string2Date(((List)v).get(0).toString(),DateUtil.yyyy_MM_ddHHmmss):((List)v).get(0)),
                (k.endsWith("time")?DateUtil.string2Date(((List)v).get(1).toString(),DateUtil.yyyy_MM_ddHHmmss):((List)v).get(1))));

        // 不等于
        Map<String, Object> neMap = Optional.ofNullable((whereCondition).get("ne")).orElse(new HashMap<>());
        neMap.forEach((k,v)->((List)v).stream().forEach(realValue->wrapper.ne(k,realValue)));

        // 大于
        Map<String, Object> gtMap = Optional.ofNullable((whereCondition).get("gt")).orElse(new HashMap<>());
        gtMap.forEach((k,v)->((List)v).stream().forEach(realValue->wrapper.gt(k,realValue)));
        // 大于等于
        Map<String, Object> geMap = Optional.ofNullable((whereCondition).get("ge")).orElse(new HashMap<>());
        geMap.forEach((k,v)->((List)v).stream().forEach(realValue->wrapper.ge(k,realValue)));
        // 小于
        Map<String, Object> ltMap = Optional.ofNullable((whereCondition).get("lt")).orElse(new HashMap<>());
        ltMap.forEach((k,v)->((List)v).stream().forEach(realValue->wrapper.lt(k,realValue)));
        // 小于等于
        Map<String, Object> leMap = Optional.ofNullable((whereCondition).get("le")).orElse(new HashMap<>());
        leMap.forEach((k,v)->((List)v).stream().forEach(realValue->wrapper.le(k,realValue)));
        // not like
        Map<String, Object> notLikeMap = Optional.ofNullable((whereCondition).get("notLike")).orElse(new HashMap<>());
        notLikeMap.forEach((k,v)->wrapper.notLike(k,v));
        // in
        Map<String, Object> inMap = Optional.ofNullable((whereCondition).get("in")).orElse(new HashMap<>());
        inMap.forEach((k,v)->wrapper.in(k,((List)v).toArray()));

        // notIn
        Map<String, Object> notInMap = Optional.ofNullable((whereCondition).get("notIn")).orElse(new HashMap<>());
        notInMap.forEach((k,v)->wrapper.notIn(k,((List)v).toArray()));
        // isNull
        Map<String, Object> isNullMap = Optional.ofNullable((whereCondition).get("isNull")).orElse(new HashMap<>());
        isNullMap.forEach((k,v)->wrapper.isNull(k));
        // isNull
        Map<String, Object> isNotNullMap = Optional.ofNullable((whereCondition).get("isNotNull")).orElse(new HashMap<>());
        isNotNullMap.forEach((k,v)->wrapper.isNotNull(k));
    }




}
